//
//  LMSlideTab.m
//  今日头条
//
//  Created by longmin on 16/6/14.
//  Copyright © 2016年 longmin. All rights reserved.
//



#import "LMSlideTab.h"
#import "LMSlideTabCell.h"

@interface LMSlideTab()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(strong, nonatomic) UICollectionView *collectionView;

@property(strong, nonatomic) UIView *bgView;

@property(nonatomic, assign) BOOL clickFlag;

@end

@implementation LMSlideTab
#pragma mark - 懒加载

-(UIView *)bgView{
    if (_bgView == nil) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor yellowColor];
//        _bgView.layer.anchorPoint = CGPointZero;
    }
    return _bgView;
}

-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        
        CGRect rect = CGRectZero;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:flowLayout];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView.backgroundColor = [UIColor clearColor];
//        _collectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
//        _collectionView.contentOffset = CGPointMake(15, 0);
        _collectionView.showsHorizontalScrollIndicator = NO;
    }
    return _collectionView;
}

- (void)setSelectColor:(UIColor *)selectColor {
    _selectColor = selectColor;
    self.bgView.backgroundColor = selectColor;
}

#pragma mark - 系统方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //添加collectionView
        [self.collectionView addSubview:self.bgView];
        [self addSubview:self.collectionView];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        [self.collectionView registerClass:[LMSlideTabCell class] forCellWithReuseIdentifier:@"LMSlideTabCell"];
        self.selectIndex = 0;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)setSelectIndex:(NSInteger)selectIndex{
    _selectIndex = selectIndex;
    [self setNeedsLayout];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.selectIndex inSection:0];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    
    CGFloat centX = cell.centerX;
    
    if (self.clickFlag) {
        
        [UIView animateWithDuration:0.2 animations:^{
            
            self.bgView.centerX = centX;
            self.bgView.bottom = self.height;
        }];
    }else{
        self.bgView.centerX = centX;
        self.bgView.bottom = self.height;
    }
    
}

-(void)setTitleArr:(NSArray *)titleArr{
    _titleArr = titleArr;
    [self setNeedsLayout];
}

#pragma mark - UICollectionViewDelegateFlowLayout
/** 指定每个cell的frame */
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    NSString *title = self.titleArr[indexPath.row];
    
    NSDictionary *attributes = @{
                                 NSFontAttributeName : self.titleFont?self.titleFont:[UIFont systemFontOfSize:13],
                                 };
    
    CGRect rect = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, self.bounds.size.height)
                                      options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:attributes
                                      context:nil];
    
    rect.size.width += 20;
    rect.size.height = self.height;
    
    self.bgView.width = rect.size.width;
    self.bgView.height = rect.size.height;
    
    
    return rect.size;
}

#pragma mark - collectionViewDataSource
/** 返回有多少个cell */
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if(!self.titleArr.count){
        NSLog(@"LMSlideTab的titleArr属性必须赋值!!!");
    }

    return self.titleArr.count;
}
/** 返回指定索引的cell */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"LMSlideTabCell";
    
    LMSlideTabCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[LMSlideTabCell alloc] init];
    }
    
    if (self.titleArr.count) {
        [cell setTitle:self.titleArr[indexPath.row]
            titleColor:self.titleColor ? self.titleColor : [UIColor blackColor]
           selectColor:self.selectColor ? self.selectColor : [UIColor orangeColor]
                  font:self.titleFont ? self.titleFont : [UIFont systemFontOfSize:13]];
    }else{
        [cell setTitle:@"未分配"
            titleColor:self.titleColor ? self.titleColor : [UIColor blackColor]
           selectColor:self.selectColor ? self.selectColor : [UIColor orangeColor]
                  font:self.titleFont ? self.titleFont : [UIFont systemFontOfSize:13]];
    }
    
    
    cell.lm_select = indexPath.row == self.selectIndex;

    return cell;
}
#pragma mark - UICollectionViewDelegate
/** cell点击 */
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.selectIndex == indexPath.row) {
        return;
    }
    self.clickFlag = YES;
    
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
    NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:self.selectIndex inSection:0];
    LMSlideTabCell *selCell = (LMSlideTabCell *)[collectionView cellForItemAtIndexPath:selectIndexPath];
    selCell.lm_select = NO;
    
    LMSlideTabCell *cell = (LMSlideTabCell *)[collectionView cellForItemAtIndexPath:indexPath];

    cell.lm_select = YES;
    [collectionView setNeedsLayout];
    if ([self.delegate respondsToSelector:@selector(slideTab:didSelectIndex:)]) {
        [self.delegate slideTab:self didSelectIndex:indexPath.row];
        self.selectIndex = indexPath.row;
    }
    
    
    
    
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LMSlideTabCell *cccell = (LMSlideTabCell *)cell;
    cccell.lm_select = NO;
    [cccell setNeedsLayout];
}


@end
